﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows;
using System.Diagnostics;
using System.Windows.Media;

namespace UML_Creator.Model
{
    public class LineConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            PathFigureCollection pfc = new PathFigureCollection();

            int ax = int.Parse(values[0].ToString());
            int ay = int.Parse(values[1].ToString());
            int ax_ = int.Parse(values[2].ToString());
            int ay_ = int.Parse(values[3].ToString());

            int bx = int.Parse(values[4].ToString());
            int by = int.Parse(values[5].ToString());
            int bx_ = int.Parse(values[6].ToString());
            int by_ = int.Parse(values[7].ToString());


            Point aBase = new Point(ax + ax_ / 2, ay + ay_ / 2);
            Point bBase = new Point(bx + bx_ / 2, by + by_ / 2);

            
            double angle = Math.Atan2(bBase.X-aBase.X, bBase.Y-aBase.Y) *  180.0 / Math.PI;
            
            List<PathSegment> l1 = new List<PathSegment>();
            List<PathSegment> l2 = new List<PathSegment>();
            PathFigure pf = new PathFigure();

            //Generate the segments
            
            //Direction right
            if (135> angle && angle > 45)
            {
                pf.StartPoint = new Point(ax + ax_, ay + (ay_/2));//Right anchor
                pf.Segments.Add(new LineSegment(new Point((ax+ax_ + bx) / 2, ay + (ay_ / 2)), true));
                pf.Segments.Add(new LineSegment(new Point((ax + ax_ + bx) / 2, by + (by_ / 2)), true));
                pf.Segments.Add(new LineSegment(new Point(bx, by + (by_ / 2)), true)); //Left anchor
            }

            //Direction left (Right inversed)
            if (-45 > angle && angle > -135)
            {
                pf.StartPoint = new Point(bx + bx_, by + (by_ / 2));
                pf.Segments.Add(new LineSegment(new Point((bx + bx_ + ax) / 2, by + (by_ / 2)), true));
                pf.Segments.Add(new LineSegment(new Point((bx + bx_ + ax) / 2, ay + (ay_ / 2)), true));
                pf.Segments.Add(new LineSegment(new Point(ax, ay + (ay_ / 2)), true));
            }

            //Direction up
            if (-135 > angle && angle > -180 || 135 < angle && angle < 180)
            {
                pf.StartPoint = new Point(bx + bx_/2, by + by_);//Buttom anchor
                pf.Segments.Add(new LineSegment(new Point(bx + bx_ / 2, (by + by_+ay)/2), true));
                pf.Segments.Add(new LineSegment(new Point(ax+ax_/2, (by + by_ + ay) / 2), true));
                pf.Segments.Add(new LineSegment(new Point(ax + ax_/ 2, ay), true)); //Top anchor
            }
            
            //Direction down (Up inversed)
            if ( 45 > angle && angle > -45)
            {
                pf.StartPoint = new Point(ax + ax_ / 2, ay + ay_);//Buttom anchor
                pf.Segments.Add(new LineSegment(new Point(ax + ax_ / 2, (ay + ay_ + by) / 2), true));
                pf.Segments.Add(new LineSegment(new Point(bx + bx_ / 2, (ay + ay_ + by) / 2), true));
                pf.Segments.Add(new LineSegment(new Point(bx + bx_ / 2, by), true)); //Top anchor
            }
            
            pfc.Add(pf);
            return pfc;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
